//
//  SharingDetailController.swift
//  BodyBuilding
//
//  Created by franze on 2017/10/20.
//  Copyright © 2017年 franze. All rights reserved.
//

import UIKit

class SharingDetailController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var infoArray:NSMutableArray!
    var shaituID:String!
    var userID:String!
    var field:UITextField!
    var tableView:UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewConfig()
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor.white
        
        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
        navBar.initialize(text: "评论")
        view.addSubview(navBar)
        
        tableView = UITableView(frame: CGRect(x: 0, y: 64, width: WIDTH, height: HEIGHT-113))
        tableView.backgroundColor = view.backgroundColor
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.tableFooterView = UIView()
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        
        let commentView = UIView(frame: CGRect(x: 0, y: HEIGHT-49, width: WIDTH, height: 49))
        commentView.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
        commentView.layer.shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: WIDTH, height: 2)).cgPath
        commentView.layer.shadowColor = UIColor.gray.cgColor
        commentView.layer.shadowOffset = CGSize(width: 0, height: -1)
        commentView.layer.shadowOpacity = 0.6
        commentView.layer.shadowRadius = 1
        view.addSubview(commentView)
        
        field = UITextField(frame: CGRect(x: 10, y: 0, width: WIDTH*0.7, height: 30))
        field.center.y = 49/2
        field.placeholder = "请输入"
        field.backgroundColor = UIColor.white
        field.layer.cornerRadius = 5
        field.layer.masksToBounds = true
        field.font = UIFont.systemFont(ofSize: 16)
        commentView.addSubview(field)
        
        let postBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 54, height: 30))
        postBtn.layer.borderColor = UIColor.lightGray.cgColor
        postBtn.layer.borderWidth = 0.5
        postBtn.layer.cornerRadius = 5
        postBtn.layer.masksToBounds = true
        postBtn.setTitle("回 复", for: .normal)
        postBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        postBtn.setTitleColor(UIColor.brown, for: .normal)
        postBtn.center = CGPoint(x: 0.85*WIDTH, y: 49/2)
        commentView.addSubview(postBtn)
        
        postBtn.addTarget(self, action: #selector(post), for: .touchUpInside)
        navBar.backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
    }
    
    @objc func post(){
        let parameter:NSDictionary = [
            "SessionID"     :   LoginModel.sessionID,
            "mod"           :   "shaireply",
            "shaitu_id"     :   shaituID,
            "reply_userid"  :   LoginModel.userID,
            "userid"        :   userID,
            "reply"         :   field.text!
        ]
        Networking.shared.creat(parameter: parameter) {
            let popup = Popup()
            popup.appear(title: "提交成功", action: nil)
            let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = DateFormatter.Style.full
            dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
            let time = dateFormatter.string(from: Date())
            let dict:NSDictionary = ["reply":self.field.text!,"reply_time":time]
            self.infoArray.add(dict)
            self.tableView.reloadData()
            self.field.text = ""
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return infoArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
        
        let dict = infoArray[indexPath.row] as! NSDictionary
        cell.textLabel?.text = dict.value(forKey: "reply") as? String
        cell.detailTextLabel?.text = dict.value(forKey: "reply_time") as? String
        return cell
    }
    
    @objc func back(){
        _ = navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
